package cs235.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class PersonIO {

    /**
     * Saves an array of Person objects to a file
     * 
     * @param people
     *            the array of Person objects to be stored in the file
     * @param fileName
     *            the name of the file in which to store the Person objects
     * @return true if the Person objects were successfully written<br />
     *         to the file, or false if an error occurred
     * @throws IllegalArgumentException
     *             if people and/or fileName is null
     */
    public static boolean save(Person[] people, String fileName) {
        if (people == null) {
            throw new IllegalArgumentException("People cannot be null");
        } else if (fileName == null){
            throw new IllegalArgumentException("Filename cannot be null");
        }
        Integer count = people.length;
        File file = new File(fileName);

        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file.getName()));
            bw.write(count.toString());
            bw.newLine();
            for (Person person : people) {
                bw.write(person.toString());
                bw.newLine();
            }
            bw.close();
            return true;
        } catch (FileNotFoundException e) {
            // System.out.println("The file you provided was not found");
        } catch (IOException e) {
            // System.out.println("The file you provided was not found");
        }
        return false;
    }

    /**
     * Loads the Person objects stored in the specified file<br />
     * and returns them in an array.
     * 
     * @param fileName
     *            the name of the file to be loaded
     * @return an array of Person objects in the same order that<br />
     *         they appear in the file. If the file cannot be opened<br />
     *         or the contents of the file are invalid, null is returned
     * @throws IllegalArgumentException
     *             if fileName is null
     */
    public static Person[] load(String fileName) {
        if (fileName == null) {
            throw new IllegalArgumentException("Filename cannot be null");
        }
        File file = new File(fileName);
        Person[] people = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader is = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(is);
            return getPeople(br);

        } catch (FileNotFoundException e) {
            // System.out.println("The file you provided was not found");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Person[] getPeople(BufferedReader br) {

        try {
            String line;
            line = br.readLine();
            Integer count = Integer.parseInt(line);
            Integer counter = 0;

            Person[] people = new Person[count];
            while (line != null && counter < count) {
                line = br.readLine();
                if (line.isEmpty()) {
                    continue;
                } else if (line.equals("employee")) {
                    people[counter] = Factory.createEmployee(br.readLine(),
                            br.readLine(), br.readLine());
                } else if (line.equals("student")) {
                    people[counter] = Factory.createStudent(br.readLine(),
                            br.readLine(), br.readLine(), Double.parseDouble(br.readLine()));
                }
                counter++;
            }
            br.close();
            return people;
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
